/**
 * Created by Xiaoming on 2018/1/29.
 */
/**
 * Created by Xiaoming on 2018/1/3.
 */
angular.module('module.recommended')
    .controller('ReCodeController', ReCodeController);
ReCodeController.$inject = ['$scope', '$state', '$localStorage', '$stateParams', 'RecommendedService'];
function ReCodeController($scope, $state, $localStorage, $stateParams, RecommendedService) {

    var vm = this;
    if (!$localStorage[environmental + "shopid"]) {
        $localStorage.$reset();
        $state.go('login');
    }
    vm.shopid = $stateParams.shopid;
    vm.foodid = $stateParams.foodid;
    vm.edit = false;
    vm.industrys = [
        {"num": 3},
        {"num": 5},
        {"num": 10}
    ];
    vm.sureBtn = sureBtn; // 模态框
    vm.cancelBtn = cancelBtn; // 模态框
    vm.cancelChange = cancelChange;
    vm.saveChange = saveChange;
    function sureBtn() {
        vm.messageVisible = false;
    }

    function cancelBtn() {
        vm.messageVisible = false;
    }

    // 获取推荐菜品信息
    RecommendedService.getFoodInfo(vm.shopid).$promise.then(function (data) {
        vm.foodinfoList = data;
        for (var i = 0; i < data.length; i++) {
            if (vm.foodid == data[i].foodid) {
                vm.foodinfo = data[i];
                setQrCode(data[i].qr);
            }
        }
    }, function (error) {
        console.log(error);
    });
    vm.codeType = 2;
    vm.shopTable = {};
    //
    // 显示二维码
    function showCode(advert, init) {
        var box = document.getElementById("box");
        var qrcode = null;
        if (box) {
            $(box).find("img").remove();
            $(box).find("canvas").remove();
            qrcode = new QRCode(box, {
                width: advert.size, // 设置宽高
                height: advert.size
            });
            $(box).css({left: advert.x, top: advert.y, width: advert.size, height: advert.size});
            qrcode.makeCode(advert.info);
            vm.foodinfo.x = parseInt(advert.x / vm.ratio);
            vm.foodinfo.y = parseInt(advert.y / vm.ratio);
            vm.foodinfo.size = parseInt(advert.size / vm.ratio);
            if (init) {
                new ImgDrg({
                    "boxId": "box",
                    "parentboxId": "big",
                    "coorId": "coor",
                    "boxMove": boxMove,
                    "coorMove": coorMove,
                    "upFuc": showCode,
                    "advert": advert
                });
            }
        }
    }

    // 二维码拖拽
    function boxMove(left, top, advert) {
        $scope.$apply(function () {
            advert.x = left;
            advert.y = top;
        });
    }

    function coorMove(width, left, top, advert) {
        advert.size = width;
        $scope.$apply(function () {
            advert.x = left;
            advert.y = top;
        });
    }

    // 页面显示二维码
    function setQrCode(param) {
        var parentBox = $(".meal_code_content_detail");
        var parentWidth = parentBox.width(), parentHeight = parentBox.height();
        var ratio = parentWidth / 800, size = 86 * ratio;
        vm.ratio = ratio;

        if (!param) {
            param = {
                x: parentWidth - size - 50 * ratio,
                y: parentHeight - size - 130 * ratio,
                size: size,
            }
        }else{
            if (typeof param == "string") {
                param = JSON.parse(param);
            }
            param.x = param.x * ratio;
            param.y = param.y * ratio;
            param.size = 86 * ratio;
        }
        if (param.x < 0 || param.y < 0) {
            param.x = parentWidth - size - 50 * ratio;
            param.y = parentHeight - size - 130 * ratio;
        }
        param.info = "二维码拖拽";
        showCode(param, true);
    }

    function cancelChange() {
        window.history.back();
    }

    // 保存
    function saveChange() {
        RecommendedService.addFoodInfo(vm.foodinfoList, function (data) {
            alert("保存成功");
            cancelChange();
        }, function (error) {
            vm.messageVisible = true;
            vm.message = "修改失败!";
        });
    }
}